package org.example.rental.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.rental.common.Result;
import org.example.rental.common.User;
import org.example.rental.domain.Car;
import org.example.rental.domain.RentalOrder;
import org.example.rental.domain.SysUser;
import org.example.rental.domain.vo.ChartVo;
import org.example.rental.domain.vo.RentalOrderVo;
import org.example.rental.service.CarService;
import org.example.rental.service.RentalOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class RentalOrderController extends BaseController {

    @Autowired
    RentalOrderService rentalOrderService;


    @Autowired
    CarService carService;

    @GetMapping("/info/{id}")
    public Result info(@PathVariable(name = "id") Long id){
        RentalOrder rentalOrder = rentalOrderService.getById(id);
        RentalOrderVo rentalOrderVo = new RentalOrderVo();
        BeanUtils.copyProperties(rentalOrder,rentalOrderVo);
        Car car = carService.getById(rentalOrderVo.getCarId());
        rentalOrderVo.setCarColor(car.getCarColor());
        return Result.succ(rentalOrderVo);
    }


    @GetMapping("/list")
    public Result list(String orderId,String carNum,String username
        ,String status,String key){
        QueryWrapper<RentalOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(orderId),"order_id",orderId);
        queryWrapper.eq(StrUtil.isNotBlank(carNum),"car_num",carNum);
        queryWrapper.eq(StrUtil.isNotBlank(username),"username",username);
        queryWrapper.eq(StrUtil.isNotBlank(status),"status",status);
        queryWrapper.like(StrUtil.isNotBlank(key),"model_num",key);
        queryWrapper.ne("status",-1);

        Page<RentalOrder> page = rentalOrderService.page(getPage(),queryWrapper);

        return Result.succ(page);
    }




    @PostMapping("/save")
    public Result save(@Validated @RequestBody RentalOrder order){

        rentalOrderService.save(order);
        return Result.succ(order);
    }


    @PostMapping("/update")
    public Result update(@Validated @RequestBody RentalOrder order){
        rentalOrderService.updateById(order);
        return Result.succ(order);
    }


    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestBody Long[] ids){
        //删除
        rentalOrderService.removeByIds(Arrays.asList(ids));


        return Result.succ("d订单删除成功");

    }


    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody RentalOrder order){
        rentalOrderService.submitOrder(order);
        return Result.succ(order);
    }


    @Autowired
    User user;

    @GetMapping("/meOrderList")
    public Result meOrderList(String status,String orderId){
        SysUser sysUser = user.getSysUser();
        QueryWrapper<RentalOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",sysUser.getId());
        queryWrapper.eq(StrUtil.isNotBlank(status),"status",status);
        queryWrapper.eq(StrUtil.isNotBlank(orderId),"order_id",orderId);
        queryWrapper.ne("status",-1);
        List<RentalOrder> list = rentalOrderService.list(queryWrapper);
        return Result.succ(list);
    }

    //支付
    @PostMapping("/pay/{id}")
    public Result orderPay(@PathVariable("id") Integer id){
        RentalOrder order = rentalOrderService.getById(id);
        order.setStatus(1);
        order.setPayTime(new Date());
        rentalOrderService.updateById(order);
        return Result.succ(order);
    }

    @PostMapping("/cancel/{id}")
    public Result cancelOrder(@PathVariable("id") Integer id){

        rentalOrderService.cancelOrder(id);
        return Result.succ("取消订单成功");
    }



    @GetMapping("/chart")
    public Result chart(){
        ChartVo chartVo = rentalOrderService.chart();
        return Result.succ(chartVo);
    }






}
